Skip to content

Support GS1-128 date AIs with a four-digit year (7250, 7251) - #498

Closed
gaoflow wants to merge 1 commit into
arthurdejong:masterfrom
gaoflow:gs1-128-four-digit-year-dates
Closed

Support GS1-128 date AIs with a four-digit year (7250, 7251)#498
gaoflow wants to merge 1 commit into
arthurdejong:masterfrom
gaoflow:gs1-128-four-digit-year-dates

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

GS1 Application Identifiers 7250 (DOB) and 7251 (DOB TIME) carry a four-digit
year (N8 = YYYYMMDD, N12 = YYYYMMDDhhmm), but gs1_128 only handled
two-digit-year date formats, so both AIs were broken in either direction:

  • info('725019800715') raised a bare ValueError: time data '19800715' does not match format '%y%m%d%H'. The N8 value fell through to the generic
    '%y%m%d%H%M%S'[:len(value)] branch, which parsed the four-digit year as YY.
  • info('7251198007151430') raised ValueError because the 12-digit N12
    value was treated as two YYMMDD dates and '198007' is not a valid date.
  • encode({'7250': datetime.date(1980, 7, 15)}) raised
    ValueError: unsupported format: N8.

_encode_date() now formats N8 as %Y%m%d and N12 as %Y%m%d%H%M, and
_decode_date() parses them back with the matching four-digit-year formats.
N12 is also removed from the two-date branch: the only date AI that encodes a
pair of YYMMDD dates is 7007 (N6[+N6]), whereas 7251 is a single datetime.

Round-trip doctests for both AIs are added to tests/test_gs1_128.doctest.

@gaoflow

gaoflow commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated the branch to pin mypy<2 in the tox mypy environment. The failing CI was installing mypy 2.1, which no longer supports the existing --python-version 3.9 target in this project. This keeps the current 3.9 type-check target working without changing runtime code.

Local verification:

  • uvx --python 3.13 tox -e mypy --skip-missing-interpreters false
  • uvx --python 3.13 tox -e flake8 --skip-missing-interpreters false
  • uvx --python 3.13 --with pytest --with pytest-cov pytest --doctest-modules --doctest-glob='*.doctest' stdnum/gs1_128.py tests/test_gs1_128.doctest

AI 7250 (DOB, N8 YYYYMMDD) and 7251 (DOB TIME, N12 YYYYMMDDhhmm) use a
four-digit year, but _encode_date() and _decode_date() only handled
two-digit-year formats. Encoding raised "unsupported format: N8" and
decoding raised a bare ValueError: N8 was parsed with '%y%m%d%H' and N12
was mistaken for two YYMMDD dates.

Handle N8 and N12 explicitly in both directions and stop treating N12 as
a pair of dates (only N6[+N6] / N6..12 encode two dates).
@gaoflow
gaoflow force-pushed the gs1-128-four-digit-year-dates branch from 6920e0e to 46b82d8 Compare July 26, 2026 17:05
@gaoflow

gaoflow commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on master; I dropped my tox.ini mypy pin from this branch since you already capped mypy the same way in b99cfad, so the PR is now just the GS1-128 change.

@arthurdejong

Copy link
Copy Markdown
Owner

Hi @gaoflow,

Thanks for providing this fix. I've merged it as 8a212c7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants